﻿using ScFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 当接收到按键消息时调用道具上相应函数
/// </summary>
public partial class FuncManager :EntityInstanceMgr<SFuncBase>,IFuncManager
{
    #region DoKeyAction
    static List<Func<KeyCode,bool>> _useableKeyCheckers = new List<Func<KeyCode, bool>>
    {
        (KeyCode k) =>
        {
            return k<=KeyCode.Z&&k>=KeyCode.A;
        },
    };
    public static bool CanKeyUse(KeyCode k)
    {
        foreach(var x in _useableKeyCheckers)
        {
            if (x(k)) return true;
        }
        return false;

    }
    HashSet<KeyCode> keySet;
    public override void Init(SubmoduleBase p, object args = null)
    {
        base.Init(p, args);
        keySet = new HashSet<KeyCode>();
        KeyRequestSet = new HashSet<KeyEventPair>();
        foreach(var x in Enum.GetValues(typeof(KeyCode)))
        {
            KeyCode k = (KeyCode)x;
            if (CanKeyUse(k))
            {
                keySet.Add(k);
            }
        }
    }

    /*public override void OnDeleted(SFuncBase comp)
    {

        base.OnDeleted(comp);
        if (KeyRequestSet.Contains(new KeyEventPair((KeyCode)comp.Key, comp.Body.UserID)))
        {
            comp.ServerKeyUp();
            comp.LocalKeyUp();
        }
    }*/
    public void OnKeysAndDoKeyAction(List<KeyEventPair> keyEventPairs)
    {
        foreach(var x in keyEventPairs)
        {
            if (KeyRequestSet.Contains(x))
            {
                continue;
            }
            else
            {
                KeyRequestSet.Add(x);
            }
        }
        List<KeyEventPair> delList = new List<KeyEventPair>();
        foreach(var x in KeyRequestSet)
        {
            if (keyEventPairs.Contains(x)==false)
            {
                delList.Add(x);
            }
        }


        foreach(var x in delList)
        {
            KeyRequestSet.Remove(x);
        }
      //  KeyRequestSet = new HashSet<KeyEventPair> { new KeyEventPair(KeyCode.F, 1) };

        foreach(var x in KeyRequestSet)
        {
            foreach (var c in _compsDic)
            {
                if (c.Value.Key == x.key && x.userID == GetInnerModule<IBelongUser>().GetBelongUser(c.Value.EntityId))
                {

                    ///目前不考虑联机,同时执行;
                    c.Value.ServerKeyOn();
                    c.Value.LocalKeyOn();
                }
            }
        }
    }
    HashSet<KeyEventPair> KeyRequestSet;
    public override void OnDestroy()
    {
        base.OnDestroy();
        KeyRequestSet = null;
        keySet = null;
    }
    #endregion
    public override void OnCreated(SFuncBase comp)
    {
        base.OnCreated(comp);
        comp.SetFuncAPI(this);
    }
    public override void OnDeleted(SFuncBase comp)
    {
        base.OnDeleted(comp);
        comp.RemoveFuncAPI();
    }

}
public struct KeyEventPair
{
    public KeyCode key;

    public int userID;

    public KeyEventPair(KeyCode k,int u)
    {
        key = k;
        userID = u;
    }




}
public interface IFuncManager
{
    void OnKeysAndDoKeyAction(List<KeyEventPair> keyEventPairs);

}